// pages/goodsManagement/goodsManagementIndex.js
const controllers = require('../controllers/controllerIndex.js').controller;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    labels: [{
      name: '1',
      id: 1
    }],
    multiIndex: [0, 0],
    rightList:[],
    labelId: 0,
    navchoice: 0,
    imgurl: 'test',
    showActionsheet: true,
    groups: [['自定义添加商品', '选择商品模板'], ["服务", "商品"]],
    multiArray: [['自定义添加商品', '选择商品模板'], ['服务', '商品']],
  
    pageNumber: 1,
    type: 0,
    isCustom: false
  },
  //跳转编辑
  toComfirmOrder(e){
    let id = e.currentTarget.dataset['id'];
    wx.navigateTo({
      url: '/pages/goodsManagement/cloudGoodsDetails/cloudGoodsDetails?id=' + id ,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onShow: function (options) {
    var that =this
    controllers.selectLabel({
      type: 3,//0默认1商城页面分类列表2店铺入驻分类列表3系统新增商城分类
      // shopId:wx.getStorageSync('shopId'),
      haveGoods: 'YES'
    }).then(res =>{
      that.setData({
        labels:res.data || [],
        labelId: res && res.data && res.data.length ? res.data[0].id : 0
      })
      that.getProduct()
    })
  },
  setUporLo(res){
    var that =this
    var state=res.currentTarget.dataset.state;
    var id  =res.currentTarget.dataset.id;
    controllers.setGoodsState({
      sessionId: wx.getStorageSync('token'),
      shopId:wx.getStorageSync('shopId'),
      goodsId:id,
      state:state === 0?0:1
    }).then(res =>{
      if(state=='0'){
        wx.showToast({
          title: '下架成功',
          icon: 'none',
          duration: 2000
        })
      }else{
        wx.showToast({
          title: '上架成功',
          icon: 'none',
          duration: 2000
        })
      }
     
       
      let index = that.data.rightList.findIndex(item => {
        return item.id == id;
      });

      
      let nowList=that.data.rightList;
      nowList[index].goodsState = 0
      that.setData({
        rightList:nowList
      })
    
     
  //    rightList: res.data.list
      
    })
  },

  setCloudUp(e){
    console.log(e)
    var that =this
    var id = e.currentTarget.dataset.id;
    var state = e.currentTarget.dataset.state;
    let rightList = [ ...that.data.rightList ];
    console.log('rightList==', rightList);
    controllers.setCloudUp({
      sessionId: wx.getStorageSync('token'),
      shopId:wx.getStorageSync('shopId'),
      goodsId: id
    }).then(res =>{
      wx.showToast({
        title: '上架成功',
        icon: 'none',
        duration: 2000
      })
      console.log('rightList==', rightList);
      
      rightList.filter((item, index) => {
        if(item.id === id){
          rightList[index].goodsState = 0;
          that.setData({
            rightList
          })
        }
      });
    })
  },
  setCloudLow(e){
    console.log(e)
    var that = this;
    var id = e.currentTarget.dataset.id;
    var state = e.currentTarget.dataset.state;
    let rightList = that.data.rightList;
    controllers.setCloudLow({
      sessionId: wx.getStorageSync('token'),
      shopId:wx.getStorageSync('shopId'),
      goodsId: id
    }).then(res =>{
      wx.showToast({
        title: '下架成功',
        icon: 'none',
        duration: 2000
      })
      rightList.filter((item, index) => {
        if(item.id === id){
          rightList[index].goodsState = 1;
          that.setData({
            rightList
          })
        }
      });
    })
  },
  navchoice(e){
    console.log('navchoice=-e==', e);
    
    var that = this;
    var labelId = parseInt(e.currentTarget.id);
    if (labelId !== that.data.labelId) {
      that.setData({
        labelId,
        pageNumber: 1,
      })
      that.getProduct();
    }
  },
  getProduct(labelId){
    var that = this;
    var labelId = that.data.labelId;
    if (!labelId) {
      return;
    }
    if (that.data.loading) {
      return;
    }
    that.data.loading = true;
    var jsonInfo ={
      pageSize: 8,
      pageNumber: that.data.pageNumber,
      labelId: labelId,
      shopId:wx.getStorageSync('shopId')
    }
    wx.showLoading();
    controllers.cloutShopGoodsPage(jsonInfo).then(res =>{
      console.log('cloutShopGoodsPage-res==', res);
      wx.hideLoading();
      const { data } = res;
      var { totalRow, lastPage, firstPage, list } = data;
      var rightList = that.data.rightList;
      if (firstPage) {
        rightList = list;
      } else {
        rightList = rightList.concat(list);
      }
      that.setData({
        totalRow,
        lastPage,
        rightList,
        jsonInfo,
        loading: false
      })
    }).catch(err => {
      wx.hideLoading();
      that.setData({
        loading: false
      })
    })
  },
  //  弹窗选项
  bindPickerChange(e) {
    if (this.data.isCustom) {
      console.log("自定义")
    } else {
      wx.navigateTo({
        url: '/pages/goodsManagement/addGoods/addGoods?type=' + this.data.type,
      })
    }
  },

  bindcolumnchange(e) {
    console.log("e", e.detail)
    let isCustom
    let groups = this.data.groups
    if (e.detail.column == 0) {
      if(e.detail.value == 0){
        isCustom = false
        groups[1]=["服务", "商品"]
      }else if(e.detail.value == 1){
        isCustom = true
        groups[1]=[]
      }
      this.setData({
        groups,
        isCustom
      })
    } else if(e.detail.column == 1){
      this.setData({
        type: e.detail.value
      })
    }
  },

  // 跳转搜索
  goodsSearch() {
    wx.navigateTo({
      url: '../goodsSearch/goodsSearch',
    })
  },

  // 跳转排序页面
  sort() {
    
   let listString = JSON.stringify( this.data.rightList);
   let jsonInfo = JSON.stringify( this.data.jsonInfo);
   
    wx.navigateTo({
      url: '../goodsSort/goodsSort?listString='+listString+'&jsonInfo='+jsonInfo,
    })
  },

  // 跳转分类管理页面
  classifcation() {
    wx.navigateTo({
      url: '../classification/classification',
    })
  },


  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // },
  onReachBottom() {
    var that = this;
    if (!that.data.loading && !that.data.lastPage) {
      that.data.pageNumber++;
      that.getProduct();
    }
  },
  bindMultiPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      multiIndex: e.detail.value
    })
  },
  bindMultiPickerColumnChange: function (e) {
   
    var data = {
      multiArray: this.data.multiArray,
      multiIndex: this.data.multiIndex
    };
    data.multiIndex[e.detail.column] = e.detail.value;
    switch (e.detail.column) {
      case 0:
        switch (data.multiIndex[0]) {
          case 0:
            data.multiArray[1] = ['服务', '商品'];
            break;
          case 1:
            data.multiArray[1] = ['选择商品模板'];
            break;
        }
        data.multiIndex[1] = 0;
        break;
    }
    
    this.setData(data);
  },
})